package com.hafu.dao;

import com.hafu.model.Khinfo;
import com.hafu.util.DbUtil;
import com.hafu.util.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class KhinfoDao {
    //添加客户信息
    public boolean addKhinfo(Khinfo kh) throws SQLException {
        String sql = "insert into khinfo values(null,?,?)";
        Connection conn = DbUtil.getConnection();
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,kh.getName());
            preparedStatement.setString(2,kh.getTel());
           //执行添加
            int i = preparedStatement.executeUpdate();
            if (i>0){
                System.out.println("添加成功");
                return true;
            }else return false;



        } catch (SQLException throwables) {
           throwables.printStackTrace();
        }
        return false;
    }
    //根据客户id删除客户
    public boolean deleteKhinfo(int id){
        String sql = "delete from khinfo where id=?";
        Connection conn = null;
        try {
            conn = DbUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            int i = preparedStatement.executeUpdate();
            if (i>0){
                System.out.println("删除成功");
                return true;
            }else return false;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //执行删除
    return false;
    }
    //根据客户名字查询客户信息
    public ArrayList<Khinfo> getKhinfoByName(Khinfo kh){
            ArrayList<Khinfo> list = new ArrayList();
        Connection conn = null;
        try {
            conn = DbUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        String sql = "select * from khinfo";
        if (!StringUtil.isEmpty(kh.getName())){
            sql = sql + " " +
                    "" +
                    "where name like '%"+kh.getName()+"%'";
        }
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String tel = resultSet.getString("tel");
                Khinfo khinfo = new Khinfo(id,name,tel);
                list.add(khinfo);
            }

        }
        catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
        }
        //修改某一个客户的信息
    public boolean updateKhinfo(Khinfo kh){
        String sql = "update khinfo set name=?,tel=? where id=?";
        Connection conn = null;
        try {
            conn = DbUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1,kh.getName());
            preparedStatement.setString(2,kh.getTel());
            preparedStatement.setInt(3,kh.getId());
            int i = preparedStatement.executeUpdate();
            if (i>0){
                System.out.println("修改成功");
                return true;
            }else return false;
        }
        catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
     }
 }


